package com.shine.commonlibrary.utils.base;


import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ProgressBar;

import com.shine.commonlibrary.R;
import com.shine.commonlibrary.utils.basemvp.BaseModel;
import com.shine.commonlibrary.utils.basemvp.BasePresenter;
import com.shine.commonlibrary.utils.utils.NetworkUtils;
import com.shine.commonlibrary.utils.utils.ToastUtils;
import com.shine.commonlibrary.utils.widget.CustomToolBar;

import io.realm.Realm;


/**
 * Created by M_genius on 2016/12/15.
 * 描述：
 *      1、加入了ToolBar的点击事件
 *      2、加入数据库初始化
 *      3、如果子Fragment用到了ToolBar，需要实现CustomToolBar.OnToolBarClickListener接口，处理点击事件
 */

public abstract class BaseOptionFragment<P extends BasePresenter, M extends BaseModel> extends BaseFragment<P, M> {


    protected Realm mRealm;

    private FrameLayout mSuperRootView;
    private ProgressBar mProgressBar;
    private View mDataView;
    public View mErrorView;

    public int option_type;

    public int getOption_type() {
        return option_type;
    }

    public void setOption_type(int option_type) {
        this.option_type = option_type;
    }


    public abstract void refreshTitleBar(CustomToolBar customToolbar);

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        mRealm = Realm.getDefaultInstance();
        //添加图层
        FrameLayout superRootView = addLoadingLayerView(view);
        return superRootView;
    }

    protected abstract void errorTipClickEvent();

    /**
     * 在Fragment的View视图上，创建了一个FrameLayout。用于添加多层View，显示不同的状态
     * 1、数据加载错误时，显示的ErrorView
     * 2、数据层mDataView
     * 3、加载数据提示层 mProgressBar
     *
     * @param view
     * @return
     */
    @NonNull
    private FrameLayout addLoadingLayerView(View view) {

        mSuperRootView = new FrameLayout(getContext());

        mDataView = view;

        //没有网络时，显示错误提示布局，点击错误提示布局，重新加载数据
        mErrorView = LayoutInflater.from(mActivity).inflate(R.layout.layout_net_error, null);
        mErrorView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (NetworkUtils.isConnected()) {
                    errorTipClickEvent();
                    setVisibleLayer(Layer.LOADING);
                } else {
                    ToastUtils.showShort("当前网络不可用，请联网后重试！");
                }
            }
        });

        mSuperRootView.addView(mErrorView);

        //第二层 数据View
        mSuperRootView.addView(mDataView);

        //第三层 进度条，在此处可以更改进度条层的样式
        mProgressBar = new ProgressBar(getContext(), null, android.R.attr.progressBarStyleInverse);
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
        mSuperRootView.addView(mProgressBar, layoutParams);

        mSuperRootView.setVisibility(View.GONE);
        mProgressBar.setVisibility(View.GONE);
        mDataView.setVisibility(View.GONE);

        //设置叠层显示那一层数据，显示数据还是显示ProgressBar
        setVisibleLayer(Layer.LOADING);

        return mSuperRootView;
    }

    public void setVisibleLayer(Layer visibleLayer) {
        switch (visibleLayer) {
            case LOADING:
                if (mProgressBar.getVisibility() != View.VISIBLE) {
                    mSuperRootView.setVisibility(View.VISIBLE);
                    mProgressBar.setVisibility(View.VISIBLE);
                    mDataView.setVisibility(View.GONE);
                    mErrorView.setVisibility(View.GONE);
                }
                break;
            case DATA:
                if (mDataView.getVisibility() != View.VISIBLE) {
                    mSuperRootView.setVisibility(View.VISIBLE);
                    mProgressBar.setVisibility(View.GONE);
                    mErrorView.setVisibility(View.GONE);
                    mDataView.setVisibility(View.VISIBLE);
                }
                break;
            case NET_ERROR:
                if (mSuperRootView.getVisibility() == View.VISIBLE) {
                    mSuperRootView.setVisibility(View.VISIBLE);
                    mErrorView.setVisibility(View.VISIBLE);
                    mProgressBar.setVisibility(View.GONE);
                    mDataView.setVisibility(View.GONE);
                }
                break;
            case EMPTY:
                if (mSuperRootView.getVisibility() == View.VISIBLE) {
                    mSuperRootView.setVisibility(View.GONE);
                    mErrorView.setVisibility(View.GONE);
                    mProgressBar.setVisibility(View.GONE);
                }
                break;
        }
    }

    public enum Layer {
        EMPTY, LOADING, NET_ERROR, DATA
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mRealm.close();
    }
}
